November 7, 2022
Introduction
Displaying maps with R packages
Displaying maps using shapefiles
Plotting locations
Plotting symbols
Plotting choropleth maps
You can disable/enable mouse click advance by pressing key 'k', when viewing the presentation.For analysis of spatial/geographical data, R can provide maps of data in the same computing environment that the data analysis is being performed in.
The main place to go to get an overview of the kinds and capabilities of the spatial packages in R is the Spatial Task Views on CRAN http://cran.us.r-project.org/web/views/Spatial.html.
sp, rgeos,
spData, shapefiles, maptools and
raster.The maps can be static or interactive.
Another classification of maps:
usmap, fiftystater, …ggmap; maps, …mapdata provides additional map databases for the
maps packageleaflet, mapview, tmap,
…tmap is an actively maintained open-source R-library
for drawing thematic maps. The API is based on A Layered Grammar of
Graphics and resembles the syntax of ggplot2.shapefiles, maptools, sf,
…Geographical points are on a sphere while maps are ploted on a flat surface.
There are two different types of Coordinate Reference Systems in GIS (Geographic Information System ).
For example,
maps and fiftystater use
geographic coordinate systems.usmap uses UTM Coordinates.For more information, see Penn State E-education UTM Coordinate System.
The function convUL() in the package
PBSmapping can be used to convert coordinates between UTM
and Lon/Lat.
The choropleth map colors/sizes regions/points by data values.
The package usmap plot US map including Alaska and Hawaii. The
function usmap::plot_usmap returns a ggplot object, which
means we can add ggplot layers to the plot right out of the
box.
The package tmap
offers a flexible, layer-based, and easy to use approach to create
thematic maps. It resembles the syntax of ggplot2.
The package leaflet https://rstudio.github.io/leaflet/ is one of the most popular open-source JavaScript libraries for mobile-friendly interactive maps.
map_data() in ggplot2 can easily turn
data from the maps package in to a data frame suitable for
plotting with ggplot2.
See ggplot2 themes for more information about themes.
## 'data.frame': 99338 obs. of 6 variables:
## $ long : num -69.9 -69.9 -69.9 -70 -70.1 ...
## $ lat : num 12.5 12.4 12.4 12.5 12.5 ...
## $ group : num 1 1 1 1 1 1 1 1 1 1 ...
## $ order : int 1 2 3 4 5 6 7 8 9 10 ...
## $ region : chr "Aruba" "Aruba" "Aruba" "Aruba" ...
## $ subregion: chr NA NA NA NA ...
## long lat group order region subregion
## 1 -69.89912 12.45200 1 1 Aruba <NA>
## 2 -69.89571 12.42300 1 2 Aruba <NA>
## 3 -69.94219 12.43853 1 3 Aruba <NA>
## 4 -70.00415 12.50049 1 4 Aruba <NA>
## 5 -70.06612 12.54697 1 5 Aruba <NA>
## 6 -70.05088 12.59707 1 6 Aruba <NA>
ggplot(worldmap, aes(long, lat, group=group)) +
geom_polygon(fill='lightblue', colour="green") + theme_bw()ttm()(which stands for toggle thematic map).maps package can be used to plot US map.usmap = ggplot2::map_data("state") # US map
ggplot(usmap, aes(long, lat, group=group, fill=region)) +
geom_polygon(show.legend = F);You have to know the names of the regions in the database though. To find that out, enter the following in the console.
“fips” is for each state’s FIPS code. FIPS stands for “Federal Information Processing Standard”, and pretty much any geography-based data from the government uses it.
## fips ssa region division abb polyname
## 1 1 1 3 6 AL alabama
## 2 4 3 4 8 AZ arizona
## 3 5 4 3 7 AR arkansas
## 4 6 5 4 9 CA california
## 5 8 6 4 8 CO colorado
## 6 9 7 1 1 CT connecticut
## 7 10 8 3 5 DE delaware
## 8 11 9 3 5 DC district of columbia
## 9 12 10 3 5 FL florida
## 10 13 11 3 5 GA georgia
## 11 16 13 4 8 ID idaho
## 12 17 14 2 3 IL illinois
## 13 18 15 2 3 IN indiana
## 14 19 16 2 4 IA iowa
## 15 20 17 2 4 KS kansas
## 16 21 18 3 6 KY kentucky
## 17 22 19 3 7 LA louisiana
## 18 23 20 1 1 ME maine
## 19 24 21 3 5 MD maryland
## 20 25 22 1 1 MA massachusetts:martha's vineyard
## 21 25 22 1 1 MA massachusetts:main
## 22 25 22 1 1 MA massachusetts:nantucket
## 23 26 23 2 3 MI michigan:north
## 24 26 23 2 3 MI michigan:south
## 25 27 24 2 4 MN minnesota
## 26 28 25 3 6 MS mississippi
## 27 29 26 2 4 MO missouri
## 28 30 27 4 8 MT montana
## 29 31 28 2 4 NE nebraska
## 30 32 29 4 8 NV nevada
## 31 33 30 1 1 NH new hampshire
## 32 34 31 1 2 NJ new jersey
## 33 35 32 4 8 NM new mexico
## 34 36 33 1 2 NY new york:manhattan
## 35 36 33 1 2 NY new york:main
## 36 36 33 1 2 NY new york:staten island
## 37 36 33 1 2 NY new york:long island
## 38 37 34 3 5 NC north carolina:knotts
## 39 37 34 3 5 NC north carolina:main
## 40 37 34 3 5 NC north carolina:spit
## 41 38 35 2 4 ND north dakota
## 42 39 36 2 3 OH ohio
## 43 40 37 3 7 OK oklahoma
## 44 41 38 4 9 OR oregon
## 45 42 39 1 2 PA pennsylvania
## 46 44 41 1 1 RI rhode island
## 47 45 42 3 5 SC south carolina
## 48 46 43 2 4 SD south dakota
## 49 47 44 3 6 TN tennessee
## 50 48 45 3 7 TX texas
## 51 49 46 4 8 UT utah
## 52 50 47 1 1 VT vermont
## 53 51 49 3 5 VA virginia:chesapeake
## 54 51 49 3 5 VA virginia:chincoteague
## 55 51 49 3 5 VA virginia:main
## 56 53 50 4 9 WA washington:san juan island
## 57 53 50 4 9 WA washington:lopez island
## 58 53 50 4 9 WA washington:orcas island
## 59 53 50 4 9 WA washington:whidbey island
## 60 53 50 4 9 WA washington:main
## 61 54 51 3 5 WV west virginia
## 62 55 52 2 3 WI wisconsin
## 63 56 53 4 8 WY wyoming
usmap includes Alaska and Hawaiilibrary(usmap)
usmap::plot_usmap(regions = "states", lines = "brown")+
theme(panel.background = element_rect(colour = "black",
fill = "lightblue"))## Warning: Ignoring unknown parameters: lines
- The function
theme() in ggplot2 is used to modify a
plot’s theme.
usmap includes Alaska and Hawaiiusmap::plot_usmap(regions = "counties",lines = "brown") +
labs(title = "Distribution of US Counties") +
theme(panel.background = element_rect(colour = "black",
fill = "lightblue"));## Warning: Ignoring unknown parameters: lines
usmap::plot_usmap(regions = "counties", include = c("PA")) +
theme(panel.background = element_rect(colour = "black",
fill = "lightblue"))usmap::plot_usmap(include = c("CA", "ID", "NV", "OR", "WA")) +
labs(title = "Western US States",
subtitle = "These are the states in the Pacific Timezone.")The disadvantage of using R packages is that you’re limited to the regions that the package offers.
Using shapefiles provides more flexibility.
Shapefiles are specifcally for geographic data. The file format encodes points, lines, and polygons in geographic space and is a common way to distribute spatial data. The file extension is .shp.
The rgdal package was very easy to use, but not
recommenced any more because it will be retired by the end of
2023.
Using the tigris package, get Census Tiger shapefiles for US census geographies. Tigris will return the shapefile in the sf, or simple features, format. (https://map-rfun.library.duke.edu/031_thematic_mapping.html)
##
|
| | 0%
|
| | 1%
|
|= | 1%
|
|= | 2%
|
|== | 2%
|
|== | 3%
|
|== | 4%
|
|=== | 4%
|
|=== | 5%
|
|==== | 5%
|
|==== | 6%
|
|===== | 6%
|
|===== | 7%
|
|===== | 8%
|
|====== | 8%
|
|====== | 9%
|
|======= | 9%
|
|======= | 10%
|
|======= | 11%
|
|======== | 11%
|
|======== | 12%
|
|========= | 12%
|
|========= | 13%
|
|========= | 14%
|
|========== | 14%
|
|========== | 15%
|
|=========== | 15%
|
|=========== | 16%
|
|============ | 17%
|
|============ | 18%
|
|============= | 18%
|
|============= | 19%
|
|============== | 19%
|
|============== | 20%
|
|============== | 21%
|
|=============== | 21%
|
|=============== | 22%
|
|================ | 23%
|
|================= | 24%
|
|================= | 25%
|
|================== | 25%
|
|================== | 26%
|
|=================== | 27%
|
|==================== | 28%
|
|==================== | 29%
|
|===================== | 30%
|
|===================== | 31%
|
|====================== | 31%
|
|====================== | 32%
|
|======================= | 32%
|
|======================= | 33%
|
|======================= | 34%
|
|======================== | 34%
|
|======================== | 35%
|
|========================= | 35%
|
|========================= | 36%
|
|========================== | 37%
|
|========================== | 38%
|
|=========================== | 38%
|
|=========================== | 39%
|
|============================ | 39%
|
|============================ | 40%
|
|============================ | 41%
|
|============================= | 41%
|
|============================= | 42%
|
|============================== | 42%
|
|============================== | 43%
|
|=============================== | 44%
|
|=============================== | 45%
|
|================================ | 45%
|
|================================ | 46%
|
|================================= | 47%
|
|================================= | 48%
|
|================================== | 48%
|
|================================== | 49%
|
|=================================== | 49%
|
|=================================== | 50%
|
|==================================== | 51%
|
|==================================== | 52%
|
|===================================== | 52%
|
|===================================== | 53%
|
|====================================== | 54%
|
|====================================== | 55%
|
|======================================= | 55%
|
|======================================= | 56%
|
|======================================== | 57%
|
|======================================== | 58%
|
|========================================= | 58%
|
|========================================= | 59%
|
|========================================== | 59%
|
|========================================== | 60%
|
|=========================================== | 61%
|
|=========================================== | 62%
|
|============================================ | 62%
|
|============================================ | 63%
|
|============================================= | 64%
|
|============================================= | 65%
|
|============================================== | 65%
|
|============================================== | 66%
|
|=============================================== | 67%
|
|=============================================== | 68%
|
|================================================ | 68%
|
|================================================ | 69%
|
|================================================= | 70%
|
|================================================== | 71%
|
|================================================== | 72%
|
|=================================================== | 72%
|
|=================================================== | 73%
|
|=================================================== | 74%
|
|==================================================== | 74%
|
|==================================================== | 75%
|
|===================================================== | 75%
|
|===================================================== | 76%
|
|====================================================== | 76%
|
|====================================================== | 77%
|
|====================================================== | 78%
|
|======================================================= | 78%
|
|======================================================= | 79%
|
|======================================================== | 80%
|
|======================================================== | 81%
|
|========================================================= | 81%
|
|========================================================= | 82%
|
|========================================================== | 82%
|
|========================================================== | 83%
|
|========================================================== | 84%
|
|=========================================================== | 84%
|
|=========================================================== | 85%
|
|============================================================ | 85%
|
|============================================================ | 86%
|
|============================================================= | 87%
|
|============================================================= | 88%
|
|============================================================== | 88%
|
|============================================================== | 89%
|
|=============================================================== | 89%
|
|=============================================================== | 90%
|
|=============================================================== | 91%
|
|================================================================ | 91%
|
|================================================================ | 92%
|
|================================================================= | 92%
|
|================================================================= | 93%
|
|================================================================== | 94%
|
|================================================================== | 95%
|
|=================================================================== | 95%
|
|=================================================================== | 96%
|
|==================================================================== | 97%
|
|==================================================================== | 98%
|
|===================================================================== | 98%
|
|===================================================================== | 99%
|
|======================================================================| 99%
|
|======================================================================| 100%
## [1] "sf" "data.frame"
## Rows: 56
## Columns: 15
## $ REGION <chr> "3", "3", "2", "2", "3", "1", "4", "1", "3", "1", "1", "3", "…
## $ DIVISION <chr> "5", "5", "3", "4", "5", "1", "8", "1", "5", "1", "1", "5", "…
## $ STATEFP <chr> "54", "12", "17", "27", "24", "44", "16", "33", "37", "50", "…
## $ STATENS <chr> "01779805", "00294478", "01779784", "00662849", "01714934", "…
## $ GEOID <chr> "54", "12", "17", "27", "24", "44", "16", "33", "37", "50", "…
## $ STUSPS <chr> "WV", "FL", "IL", "MN", "MD", "RI", "ID", "NH", "NC", "VT", "…
## $ NAME <chr> "West Virginia", "Florida", "Illinois", "Minnesota", "Marylan…
## $ LSAD <chr> "00", "00", "00", "00", "00", "00", "00", "00", "00", "00", "…
## $ MTFCC <chr> "G4000", "G4000", "G4000", "G4000", "G4000", "G4000", "G4000"…
## $ FUNCSTAT <chr> "A", "A", "A", "A", "A", "A", "A", "A", "A", "A", "A", "A", "…
## $ ALAND <dbl> 62266296765, 138958484319, 143778461053, 206232157570, 251518…
## $ AWATER <dbl> 489206049, 45975808217, 6216594318, 18949864226, 6979171386, …
## $ INTPTLAT <chr> "+38.6472854", "+28.3989775", "+40.1028754", "+46.3159573", "…
## $ INTPTLON <chr> "-080.6183274", "-082.5143005", "-089.1526108", "-094.1996043…
## $ geometry <MULTIPOLYGON [°]> MULTIPOLYGON (((-81.74725 3..., MULTIPOLYGON (((…
## # A tibble: 56 × 15
## REGION DIVISION STATEFP STATENS GEOID STUSPS NAME LSAD MTFCC FUNCSTAT
## <chr> <chr> <chr> <chr> <chr> <chr> <chr> <chr> <chr> <chr>
## 1 3 5 54 01779805 54 WV West Virg… 00 G4000 A
## 2 3 5 12 00294478 12 FL Florida 00 G4000 A
## 3 2 3 17 01779784 17 IL Illinois 00 G4000 A
## 4 2 4 27 00662849 27 MN Minnesota 00 G4000 A
## 5 3 5 24 01714934 24 MD Maryland 00 G4000 A
## 6 1 1 44 01219835 44 RI Rhode Isl… 00 G4000 A
## 7 4 8 16 01779783 16 ID Idaho 00 G4000 A
## 8 1 1 33 01779794 33 NH New Hamps… 00 G4000 A
## 9 3 5 37 01027616 37 NC North Car… 00 G4000 A
## 10 1 1 50 01779802 50 VT Vermont 00 G4000 A
## # … with 46 more rows, and 5 more variables: ALAND <dbl>, AWATER <dbl>,
## # INTPTLAT <chr>, INTPTLON <chr>, geometry <MULTIPOLYGON [°]>